
***************************************************************;
** Create 2009 denominator file matched with HEDIS 2009 data **;
***************************************************************;

options ls=76 nofmterr;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';


     *** Prepare crosswalk file ***;

   data xwalk01; set raw.hickaiser_bene_xwalk_2009;

   data xwalk02; set raw.hic_bene_xwalk_2009;

   data xwalk00; set xwalk01 xwalk02;

        if hic_match=1;
        if bene_id=' ' then delete;

        length hic_number $ 12;
        hic_number=orig_hic;

        proc sort; by hic_number;


   data xwalk0; set xwalk00;

        proc freq data=xwalk0 noprint;
             tables hic_number/out=benecnt;

        proc freq data=benecnt;
             tables count;
             title 'HEDIS2009: Num of BENE links for one HIC number';

   data xwalk(drop=count); merge xwalk00 benecnt; by hic_number;

        if count=1; /** only keep cases with one unique BENE ID **/

        keep hic_match hic_number bene_id count;

        proc sort; by bene_id;


data denom0; set raw.denom2009;

keep BENE_ID
    STATE_CODE
    BENE_COUNTY_CD
    BENE_ZIP_CD
    BENE_BIRTH_DT
    BENE_SEX_IDENT_CD
    BENE_RACE_CD 
    RTI_RACE_CD
    BENE_AGE_AT_END_REF_YR
    BENE_ENTLMT_RSN_ORIG
    BENE_ENTLMT_RSN_CURR
    BENE_ESRD_IND
    BENE_MDCR_STATUS_CD
    BENE_PTA_TRMNTN_CD
    BENE_PTB_TRMNTN_CD
    BENE_MDCR_ENTLMT_BUYIN_IND_01--BENE_MDCR_ENTLMT_BUYIN_IND_12
    BENE_HMO_IND_01--BENE_HMO_IND_12
    BENE_HI_CVRAGE_TOT_MONS
    BENE_SMI_CVRAGE_TOT_MONS
    BENE_HMO_CVRAGE_TOT_MONS
    BENE_STATE_BUYIN_TOT_MONS
    BENE_VALID_DEATH_DT_SW
    BENE_DEATH_DT
    BENE_ENROLLMT_REF_YR
    STRICT_SAMPLE_FLAG
    ENHANCED_FIVE_PERCENT_FLAG
    CRNT_BIC_CD
    DUAL_STUS_CD_01--DUAL_STUS_CD_12 ;

   ** Note: Too many variables in Original to keep all **;


  ********************************************************;
  ** Merge HEDIS dataset and Denominator file via XWALK **;
  ********************************************************;

data denom; set denom0;

     denom_flag=1;
     
     sex  = 1*BENE_SEX_IDENT_CD;
     race = 1*BENE_RACE_CD;          
     age  = BENE_AGE_AT_END_REF_YR -1;  /** convert to Age at the Begining to consistent with previous years **/

     length racec gender agec $ 10;

     if race=1 then racec='1.White';
     if race=2 then racec='2.Black';
     if race=3 then racec='3.Other';
     if race=4 then racec='4.Asian';
     if race=5 then racec='5.Hispanic';
     if race=6 then racec='6.Native';
     if race=. then racec='0.Unknown';

     if sex=1 then gender='1.Male';
     if sex=2 then gender='2.Female';

     if age>0   and age<20 then agec='1-20 yr';
     if age>=20 and age<30 then agec='20-29 yrs';
     if age>=30 and age<40 then agec='30-39 yrs';
     if age>=40 and age<50 then agec='40-49 yrs';
     if age>=50 and age<60 then agec='50-59 yrs';
     if age>=60 and age<65 then agec='60-64 yrs';
     if age>=65 and age<70 then agec='65-69 yrs';
     if age>=70 and age<75 then agec='70-74 yrs';
     if age>=75 and age<80 then agec='75-79 yrs';
     if age>=80 and age<85 then agec='80-84 yrs';
     if age>=85            then agec='85+';

     proc freq;
          tables agec gender racec;

     proc sort; by bene_id;

data new.hedis09denom; merge xwalk(in=ok) denom; by bene_id;

     if ok;

     if denom_flag=. then denom_flag=0;
        
     proc freq; tables denom_flag;


endsas;

